import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

Vue.use(VueRouter)
const routes = [{
  path: '/',
  redirect: 'login'

},
{
  //首页影片列表
  path: '/index',
  component: () => import('@/views/index.vue')
},
{
  //影片详情页
  path: '/movie-info',
  component: () => import('@/views/Movieinfo.vue')
},
{
  //后台管理登录
  path: '/login',
  component: () => import('@/views/admin-login.vue')
},
{
  //后台管理系统首页
  path: '/admin',
  component: () => import('@/views/admin-index.vue'),

},


]

const router = new VueRouter({
  routes
})

//配置路由守卫，做权限控制
router.beforeEach((to, from, next) => {
  console.log('路由守卫', to, from, next);
  var path = to.fullPath;
  console.log(path)
  if (path.indexOf('/admin') >= 0) {
    var user = localStorage.getItem("usertoken");
    console.log('user:', user);
    if (user && user.length > 0) {
      next();
    }
    else {
      next({ path: '/login' })
    }
  }
  next();
})

export default router
